package com.clsz.controller;

import com.clsz.bean.bo.SiteChooseBo;
import com.clsz.service.SiteService;
import com.clsz.utils.RedisUtils;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/site")
public class SiteController {
    @Resource
    private KafkaTemplate kafkaTemplate;
    @Resource
    private RedisUtils redisUtils;
    @Autowired
    private SiteService siteService;

    @PutMapping
    public Object chooseSite(@RequestBody SiteChooseBo siteChooseBo,
                             @RequestHeader String authorization) {
        Map<String, Object> res = new HashMap<>();
        if (redisUtils.hasKey(authorization)) {
            if (siteService.chooseSite(siteChooseBo, authorization)) {
                res.put("code", "200");
                res.put("message", "ok");
            } else {
                res.put("code", "500");
                res.put("message", "该座位已被占用");
            }
        } else {
            res.put("code", "401");
            res.put("message", "Unauthrization");
        }
        return res;
    }
}
